package turing.forum.common.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import turing.forum.common.constant.HttpStatus;
import turing.forum.common.domain.AjaxResult;
import turing.forum.common.domain.ResultsList;

import java.util.List;

public class BaseController {
    /**
     * 设置响应的结果集
     * TODO: 使用切片获取数据条数
     * @param list
     * @return
     */
    protected ResultsList getResultsList(List<?> list) {
        ResultsList rspList = new ResultsList();
        rspList.setCode(HttpStatus.SUCCESS);
        rspList.setMsg("query successfully");
        rspList.setRows(list);
        rspList.setTotal(0);
        return rspList;
    }

    protected ResultsList getResultsList(Page<?> page) {
        ResultsList rspList = new ResultsList();
        rspList.setCode(HttpStatus.SUCCESS);
        rspList.setMsg("qury successfully");
        rspList.setRows(page.getRecords());
        rspList.setTotal(page.getTotal());
        return rspList;
    }

    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error()
    {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message)
    {
        return AjaxResult.success(message);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message)
    {
        return AjaxResult.error(message);
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result)
    {
        return result ? success() : error();
    }
}
